package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	5705
 * http://news.fznews.com.cn  福州新闻网
 * 	
 * @author KK
 *
 */
public class FznewsNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		/*
		*		[0-9]*
		*       [a-z]*
		*		[a-z0-9]*
		*       [a-zA-Z0-9]*
		*/		
		
//		"http://news.fznews.com.cn/[0-9]*-[0-9]*-[0-9]*/[a-zA-Z0-9]*.shtml",
		"http://[a-z]+.fznews.com.cn/[a-z\\/]+[0-9\\-]+/.*.shtml",
		"http://[a-z]+.fznews.com.cn/[a-z]+/[0-9\\/]+[0-9a-z]+.shtml",
		"http://[a-z]+.fznews.com.cn/NewsView.aspx\\?NewsID=[0-9]+",
		"http://[a-z]+.fznews.com.cn/html/[0-9]+/[0-9\\-]+/[0-9]+.shtml"
	};	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.contains("list")) {
				return false;
			}
			if (url.contains("/more/")) {
				return false;
			}
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);				
		news.setUrl(url);	
		
		title =doc.select("div#l_box1 h1").text();	
		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title = StringUtils.substringBefore(title, "_");
		}
		if (StringUtils.isBlank(title)) {
			title =doc.select("div.NewsViewTitle").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div.zhengwen"));	
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.ad-controls"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#artibody"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#content"));
		}
		/**
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");
//		String authorStr=null;
//		authorStr=doc.select("div#laiyuan").text();
//		if(authorStr.indexOf("来源：") != -1 && authorStr.indexOf("【") != -1){
//			author=StringUtils.substringBetween(authorStr, "来源：", "【");
//		}
//		if(StringUtils.isBlank(author)){
//			author=StringUtils.substringAfter(doc.select("h2.imgtime").text(),"来源：");
//		}
		date=DateUtils.matchDate(url);
		if(date == null){
			date=DateUtils.matchDate(doc.select("div.left").text());
		}
		if(date == null){
			date=DateUtils.matchDate(doc.select("div[style=width: 100%;text-align: center;]").text());
		}
		if (StringUtils.isNotBlank(title) && date!=null){			
			news.setTitle(StringUtils.trimSpace(title));
			news.setContent(content);
			news.setAuthor(author);
			news.setDate(date);	
			news.setType(1);	
		}
		return news;
	}	
	public static void main(String[] args) {
		String url="http://xuexi.fznews.com.cn/node/10548/20151203/565f8f0573acf.shtml";
		FznewsNewsAnalyse analyse=new FznewsNewsAnalyse();		
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
